//
//  photoViewController.m
//  MoneyLog
//
//  Created by wicresoft on 5/4/11.
//  Copyright 2011 wicresoft. All rights reserved.
//

#import "photoViewController.h"


@implementation photoViewController
@synthesize imageView;
@synthesize noticeLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [imageView release];
	[noticeLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)takeNewPhoto:(id)sender
{
	UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
	
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
	{
		imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing=YES;
		//imagePicker.allowsImageEditing = YES;
		imagePicker.delegate = self;
		[self presentModalViewController:imagePicker animated:YES];
		[imagePicker release];
	}
	else
	{
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
													   message:@"Camera not available!"
													  delegate:nil
											 cancelButtonTitle:@"Close"
											 otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}

- (IBAction)selectPhotoFromSavedAlbum:(id)sender
{
	UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
	
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
	{
		imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.allowsEditing=YES;
		//imagePicker.allowsImageEditing = YES;
		imagePicker.delegate = self;
		[self presentModalViewController:imagePicker animated:YES];
		[imagePicker release];
	}
	else
	{
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil			message:@"Error accessing saved album!"
													  delegate:nil
											 cancelButtonTitle:@"Close"
											 otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}
- (IBAction)selectPhotoFromPhotoLibrary:(id)sender
{
	UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
	
	if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
	{
		imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.allowsEditing=YES;
		//imagePicker.allowsImageEditing = YES;
		imagePicker.delegate = self;
		[self presentModalViewController:imagePicker animated:YES];
		[imagePicker release];
	}
	else
	{
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil			
                                                       message:@"Error accessing photo library!"								  delegate:nil
											 cancelButtonTitle:@"Close"		
											 otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}
#pragma mark -
#pragma mark UIImagePickerControllerDelegate method
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
	[picker dismissModalViewControllerAnimated:YES];
}

//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
//{
//	noticeLabel.hidden = YES;
//	[imageView setImage:image];
//	[picker dismissModalViewControllerAnimated:YES];
//}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	noticeLabel.hidden = YES;
	UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
	[imageView setImage:image];
	[picker dismissModalViewControllerAnimated:YES];
}

-(IBAction)OK
{
    [self dismissModalViewControllerAnimated:YES];
}


@end
